package com.aviccxzx.smarthome.UserModule;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.aviccxzx.smarthome.DeviceModule.GosDeviceModuleBaseActivity;
import com.aviccxzx.smarthome.MsgID;
import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.entity.BindSucBean;
import com.aviccxzx.smarthome.netutil.app.App;
import com.aviccxzx.smarthome.service.RetrofitHelper;
import com.aviccxzx.smarthome.service.RetrofitService;
import com.aviccxzx.smarthome.utils.ChangeNameDialogUtil;
import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.google.gson.Gson;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.aviccxzx.smarthome.ConfigModule.GosAirlinkConfigCountdownActivity.gatedevice;
import static com.aviccxzx.smarthome.R.id.set_gateway_name;


/**
 * Created by joey on 2016/10/25.
 */
public class AddGateSuccActivity extends GosDeviceModuleBaseActivity implements View.OnClickListener {
    TextView gateway_name_tv;
    String gateName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.add_succ_lay_v19);
        } else {
        }
        setView();
        setListener();
        //重新添加网关的时候 判断推送服务是否停止了
        if (JPushInterface.isPushStopped(getApplicationContext())){
            //如果推送服务stoped
            JPushInterface.resumePush(getApplicationContext());
        }
        //绑定gprs
//        bindGprs();
    }

    String imei;
    String uid;
    String Mac;

    private void bindGprs() {
        uid = spf.getString("Uid", "");
        Mac = spf.getString("gatemac", "");
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Gson gson = new Gson();

        HashMap<String, String> paramsMap = new HashMap<>();

        /*{
                "imei": "string",
                "mac": "string",
                "product_key": "string",
                "product_secret": "string",
                "uid": "string"
        }*/

        paramsMap.put("imei", "");
        paramsMap.put("mac", Mac);
        paramsMap.put("product_key", MsgID.PK_GATE);
        paramsMap.put("product_secret", MsgID.PS);
        paramsMap.put("uid", uid);

        String strEntity = gson.toJson(paramsMap);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        Call<BindSucBean> call = service.bindsucess(body);
        call.enqueue(new Callback<BindSucBean>() {
            @Override
            public void onResponse(Call<BindSucBean> call, Response<BindSucBean> response) {
                if (response.body() != null && response.body().getData() != null) {
                    imei = response.body().getData().getImei();
                    if (!TextUtils.isEmpty(imei)) {
                        spf.edit().putString("Imei", imei).commit();
                    }
                }
            }

            @Override
            public void onFailure(Call<BindSucBean> call, Throwable t) {
                //                App.getApp().showToast("绑定gprs失败了");
            }
        });
    }


    private void setListener() {
        findViewById(R.id.set_gateway_name).setOnClickListener(this);
        findViewById(R.id.sure_btn).setOnClickListener(this);
    }

    private void setView() {
        ((TextView) findViewById(R.id.title_tv)).setText("添加成功");
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        gateway_name_tv = (TextView) findViewById(R.id.gateway_name_tv);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.sure_btn:
                finish();
                break;
            case set_gateway_name:
                //修改终端名称
                new ChangeNameDialogUtil(AddGateSuccActivity.this, "修改名称", new ChangeNameDialogUtil.SureInterfance() {
                    @Override
                    public void sureTodo(String name) {
                        //确定修改 处理方案
                        gateName = name;
                        gatedevice.setListener(getGizWifiDeviceListener());
                        gatedevice.setCustomInfo("", name);
                        progressDialog.show();
                    }
                }).showSelfDialog();
                break;
        }
    }

    //修改名字回调
    @Override
    public void didSetCustomInfo(GizWifiErrorCode result, GizWifiDevice device) {
        super.didSetCustomInfo(result, device);
        progressDialog.dismiss();
        if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
            // 修改成功
            App.getApp().showToast("修改成功");
            gateway_name_tv.setText(gateName);
            //                finish();
        } else {
            //修改失败
            App.getApp().showToast("修改失败");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
